#include "threadpool.h"

int initThrreadPool(threadPool_t *pthreadPool,int workerNum){
    pthreadPool->workerNum=workerNum;
    pthreadPool->tidArr=(pthread_t*)calloc(workerNum,sizeof(pthread_t));
    ERROR_CHECK(pthreadPool->tidArr,NULL,"calloc");
    initQueue(&pthreadPool->taskQueue);
    return 0;
}

int main(int argc,char* argv[])
{
    ARGS_CHECK(argc,4);
    threadPool_t threadPool;
    int workerNum=atoi(argv[3]);
    initThrreadPool(&threadPool,workerNum);
    //初始化tcp
    int sockfd;
    tcpInit(argv[1],argv[2],&sockfd);
   
    makeWorker(&threadPool);
    //监听准备工作
    int epfd=epoll_create(1);
    epollAdd(epfd,sockfd);
    struct epoll_event readyset[1024];
    //开始监听
    while(1){
        int readynum=epoll_wait(epfd,readyset,1024,-1);
        for(int i=0;i<readynum;i++){
            if(readyset[i].data.fd==sockfd){
                int netfd=accept(sockfd,NULL,NULL);
                printf("1 client connected\n");
                pthread_mutex_lock(&threadPool.taskQueue.mutex);
                enQueue(&threadPool.taskQueue,netfd);
                printf("1 netfd enqueue\n");
                pthread_cond_signal(&threadPool.taskQueue.cond);
                pthread_mutex_unlock(&threadPool.taskQueue.mutex);

            }
        }
    }
    return 0;
}

